Regular expressions allow nested arguments, and Django will resolve them and pass them to the view. When reversing, Django will try to fill in all outer captured arguments, ignoring any nested captured arguments.
Consider the following URL patterns which optionally take a page argument:. The outer argument in this case is a non-capturing argument? This coupling is even more pronounced when reversing, since to reverse the view we need to pass the piece of URL instead of the page number.
As a rule of thumb, only capture the values the view needs to work with and use non-capturing arguments when the regular expression needs an argument but the view ignores it. In the above example, both URL patterns point to the same view — views. If the first pattern matches, the page function will use its default argument for num , 1. If the second pattern matches, page will use whatever num value was captured.
This makes the system blazingly fast. The views to use for these cases are specified by four variables. Their default values should suffice for most projects, but further customization is possible by overriding their default values. See the documentation on customizing error views for the full details. Such values can be set in your root URLconf. Setting these variables in any other URLconf will have no effect.
Values must be callables, or strings representing the full Python import path to the view that should be called to handle the error condition at hand.
It includes a number of other URLconfs:. Whenever Django encounters include , it chops off whatever part of the URL matched up to that point and sends the remaining string to the included URLconf for further processing. Another possibility is to include additional URL patterns by using a list of path instances.
For example, consider this URLconf:. This can be used to remove redundancy from URLconfs where a single pattern prefix is used repeatedly.
We can improve this by stating the common path prefix only once and grouping the suffixes that differ:. In the above example, the captured "username" variable is passed to the included URLconf, as expected. URLconfs have a hook that lets you pass extra arguments to your view functions, as a Python dictionary. The path function can take an optional third argument which should be a dictionary of extra keyword arguments to pass to the view function.
This technique is used in the syndication framework to pass metadata and options to views. When this happens, the arguments in the dictionary will be used instead of the arguments captured in the URL.
Similarly, you can pass extra options to include and each line in the included URLconf will be passed the extra options. A common need when working on a Django project is the possibility to obtain URLs in their final forms either for embedding in generated content views and assets URLs, URLs shown to the user, etc.
It is strongly desirable to avoid hard-coding these URLs a laborious, non-scalable and error-prone strategy. Equally dangerous is devising ad-hoc mechanisms to generate URLs that are parallel to the design described by the URLconf, which can result in the production of URLs that become stale over time.
Among other advantages it would allow evolution of the URL design without having to go over all the project source code to search and replace outdated URLs. The primary piece of information we have available to get a URL is an identification e. Other pieces of information that necessarily must participate in the lookup of the right URL are the types positional, keyword and values of the view arguments.
You feed it with your URLconf and then it can be used in both directions:. If, for some reason, it was decided that the URLs where content for yearly article archives are published at should be changed then you would only need to change the entry in the URLconf.
In some scenarios where views are of a generic nature, a many-to-one relationship might exist between URLs and views. Read the next section to know about the solution Django provides for this. The string used for the URL name can contain any characters you like. You are not restricted to valid Python names. Putting a prefix on your URL names, perhaps derived from the application name such as myapp-comment instead of comment , decreases the chance of collision.
You can deliberately choose the same URL name as another application if you want to override a view. For example, a common use case is to override the LoginView.
Keys are seldomly unique in tables when they are foreign originated in another table to the table in question. I am not DB expert, but if you can justify logically to have an auto-generated value as your PK, I would do that. If this is not practical, then a concatenation of two or maybe more FK could serve as your PK.
It is not totally applied for the question's case, but since I ended up on this question serching for other info and by reading some comments, I can say it is possible to only have a FK in a table and get unique values. You can use a column that have classes, which can only be assigned 1 time, it works almost like and ID, however it could be done in the case you want to use a unique categorical value that distinguish each record. Stack Overflow for Teams — Collaborate and share knowledge with a private group.
Create a free Team What is Teams? Collectives on Stack Overflow. Learn more. Is it fine to have foreign key as primary key? Ask Question. Asked 9 years, 5 months ago. Active 3 months ago. Viewed k times. I have two tables: User username, password Profile profileId, gender, dateofbirth, Improve this question. Duc Tran Duc Tran 5, 4 4 gold badges 32 32 silver badges 41 41 bronze badges. Entity Framework generates that with code first for zeroOrOne and one -to- one relations. Is it the best way That's another question.
But it's valid. I never did that while creating my own databases but I even never thought to that. Add a comment. Active Oldest Votes. Improve this answer. Robert Harvey Robert Harvey k 44 44 gold badges silver badges bronze badges. A composite primary key that consists of two foreign keys is also perfectly fine for implementing many-to-many relationships.
Nothing about a foreign key dictates that it is a 1 to many or "allow duplicates" as written. Key constraints and uniqueness are two separate concepts in a database and can easily be mixed just as easily as adding an index which would be a third separate concept.
I also agree with RobertHarvey - surrogate keys make a better choice in some cases as well. For example, you could have an intermediate table that maps out a many-to-many relationship, whose primary key is used as part of another table. However, if you HAVE a surrogate key, all that is passed down is one key.
In my opinion, it is more simple and efficient. This is a rare construct, but it applies for: a relation. Profile may or may not exist, depending on the user type. There would be a negative performance if the tables were joined often, which leads to the normal recommendation that 1 table is better.
In some cases the data is always accessed separately, not joined, and there can be a organizational benefits to having two tables with a relationship. Riaj Ferdous Riaj Ferdous 6 6 silver badges 5 5 bronze badges.
Subtype tables primary key should be foreign key reference to supertype table. Change the primary key in Access.
Additional information. Access uses primary key fields to quickly associate data from multiple tables and combine that data in a meaningful way. You can include the primary key fields in other tables to refer back to the table that is the source of the primary key. In those other tables, the fields are called foreign keys. In the Customers table, it is the primary key.
In the Orders table it is called a foreign key. A foreign key, simply stated, is another table's primary key. For more information, see Database design basics. If you are moving existing data into a database, you may already have a field that you can use as the primary key.
Often, a unique identification number, such as an ID number or a serial number or code, serves as a primary key in a table. For example, you might have a Customers table where each customer has a unique customer ID number. The customer ID field is the primary key.
Access automatically creates an index for the primary key, which helps speed up queries and other operations. Access also ensures that every record has a value in the primary key field, and that it is always unique. When you create a new table in Datasheet view, Access automatically creates a primary key for you and assigns it a field name of "ID" and the AutoNumber data type. An AutoNumber field automatically generates a value for itself when each record is first saved.
Therefore, an AutoNumber field meets all three characteristics of a good primary key. For more information on adding an AutoNumber field, see the article Add an AutoNumber field as a primary key.
Any field that is missing one or more of the characteristics of a good candidate key is a poor choice for a primary key. Here are a few examples of fields that would make poor primary keys for a Contacts table, along with reasons why they would be poor choices. The fact portion might change, creating a maintenance burden. Could lead to confusion if the fact portion is repeated as a separate field. For example, combining the city and an incremented number e. In some cases, you want to use two or more fields in a table as the primary key.
A key that has more than one field is called a composite key. For a primary key to work well, the field must uniquely identify each row, never contain an empty or null value, and rarely ideally, never change. To set the primary key:. In the Navigation Pane, right click the table in which you want to set the primary key and, on the shortcut menu, click Design View.
0コメント